//
//  RBGroupPayVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/27.
//

#import "RBGroupPayVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import "RBPaySucVC.h"
#import "RBOtherPayPopVC.h"
#import "UIViewController+RBAdd.h"
#import "RBGroupDoneVC.h"
#import "UINavigationController+RBAdd.h"
#import "RBGroupAloneBuyVC.h"
#import "RBPaySucVC.h"
#import "RBGoodsOrderVC.h"

@interface RBGroupPayVC ()

@property(nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic, strong)NSMutableArray *payWayViewsArray;
@property(nonatomic, assign)NSInteger payIndex;

@end

@implementation RBGroupPayVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];

}

#pragma mark - Private

- (void)setup
{
    self.title = @"收银台";
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(10)];
        
        [view view_addView:UIView.new withHeight:Zoom(10) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:[self infoViewWithTitle:@"订单编号" des:self.orderNum sType:0] withHeight:Zoom(36) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
        [view view_addView:[self infoViewWithTitle:@"订单金额" des:self.price sType:1] withHeight:Zoom(36) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:UIView.new withHeight:Zoom(10) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(10)];
        
        [view view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(16) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"支付方式"];
            view;
        }) withHeight:Zoom(54) width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(12) right:0 centerX:0];
        
        for (NSInteger i = 0; i < 4; i++)
        {
            [view view_addView:[self payWayWithIndex:i] withHeight:Zoom(50) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
        }
        
        view;
    }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UILabel *view = [RanTool labelWithFontSize:15 weight:0 color:[UIColor whiteColor] align:1 text:@"确认支付"];
        [view rbAddTapGesWithTarget:self action:@selector(payBtnTapped)];
        view.backgroundColor = [UIColor colorWithHexString:@"#518CFF"];
        [view rbSetCornerRadius:Zoom(22)];
        view;
    }) withHeight:Zoom(44) width:Zoom(350) top:Zoom(130) bottom:Zoom(50) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    
    [self selectPayViewAtIndex:0];
}

- (void)selectPayViewAtIndex: (NSInteger)index
{
    self.payIndex = index;
    for (UIView *itemView in self.payWayViewsArray)
    {
        UIImageView *imgView = (UIImageView *)[itemView viewWithTag:33];
        imgView.image = itemView.tag == index? [UIImage imageNamed:@"addressListDefault"]: [UIImage imageNamed:@"groupPayUnsel"];
    }
}

#pragma mark - Network

#pragma mark - Event

- (void)payWayViewTapped: (UITapGestureRecognizer *)ges
{
    [self selectPayViewAtIndex:ges.view.tag];
}

- (void)payBtnTapped
{
    __weak typeof(self) weak_self = self;
    switch(self.payIndex)
    {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            
        }
            break;
        case 2:
        {
            [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
                dicM[@"order_number"] = self.orderNum;
            } path:@"hu_balance_pay" post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(id data) {
                [weak_self payFinishAction];
            } failBlock:^(RBResponse *data) {
                [weak_self.view showTip:data.message completion:nil];
            } reloadBlock:nil];
        }
            break;
        case 3:
        {
            RBOtherPayPopVC *vc = RBOtherPayPopVC.new;
            [self presentVc:vc];
        }
            break;
        default:
            break;
    }
}
//支付完成跳转页面
- (void)payFinishAction {
    UIViewController *vc;
    if (self.type == 0) {
        
        RBGoodsOrderVC *itemVc = RBGoodsOrderVC.new;
        vc = itemVc;
        
    } else if(self.type == 1) {
        
        RBGroupDoneVC *itemVc = RBGroupDoneVC.new;
        vc = itemVc;
        itemVc.type = self.type - 1;
        itemVc.group_id = self.group_id;
        itemVc.group_activity_id = self.group_activity_id;
        
    } else if(self.type == 2) {
        
        [self.navigationController popToViewControllerWithClass:RBGroupDoneVC.class animated:YES];
        return;
    }
    
    NSArray *vcStringArray = ({
        NSMutableArray *arrayM = NSMutableArray.array;
        NSArray *vcs = self.navigationController.viewControllers;
        for (UIViewController *vc in vcs) {
            [arrayM addObject:NSStringFromClass(vc.class)];
        }
        arrayM;
    });
    if([vcStringArray containsObject:NSStringFromClass(RBGroupAloneBuyVC.class)])
    {
        [self.navigationController pushToViewController:vc animated:YES removeVcCount:2];
    }
    else
    {
        [self.navigationController pushToViewController:vc animated:YES removeVcCount:1];
    }
}

#pragma mark - Setter

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            view.backgroundColor = [RBThemeConfig bgGrayColor];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _scrollView;
}

- (UIView *)infoViewWithTitle: (NSString *)title des: (NSString *)des sType: (NSInteger)sType
{
    UIView *view = UIView.new;
    
    UILabel *titleL = [RanTool labelWithFontSize:Zoom(16) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:title];
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(13));
        make.centerY.mas_equalTo(0);
    }];
    
    UILabel *desL = [RanTool labelWithFontSize:Zoom(14) weight:0 color:@"#969593" align:2 text:des];
    [view addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Zoom(12));
        make.centerY.mas_equalTo(0);
    }];
    
    if(sType == 1)
    {
        desL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
            [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:16] color:[RBThemeConfig textBlackColor] string:@"¥"];
            [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:22] color:[RBThemeConfig textBlackColor] string:des];
        }];
    }
    
    return view;
}

- (UIView *)payWayWithIndex: (NSInteger)index
{
    NSArray *imgs = @[
        @"groupPayWechat",
        @"groupPayAli",
        @"groupPayWallet",
        @"groupPayWallet",
    ];
    NSArray *names = @[
        @"微信",
        @"支付宝",
        @"余额支付",
        @"好友代付"
    ];
    UIView *view = UIView.new;
    [self.payWayViewsArray addObject:view];
    view.tag = index;
    [view rbAddTapGesWithTarget:self action:@selector(payWayViewTapped:)];
    
    UIImageView *imgView = [UIImageView.alloc initWithImage:[UIImage imageNamed:imgs[index]]];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(12));
        make.width.height.mas_equalTo(Zoom(18));
        make.centerY.mas_equalTo(0);
    }];
    
    UILabel *nameL = [RanTool labelWithFontSize:Zoom(15) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:names[index]];
    [view addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imgView.mas_right).offset(Zoom(9));
        make.centerY.mas_equalTo(0);
    }];
    
    UIImageView *selImgView = UIImageView.new;
    selImgView.tag = 33;
    [view addSubview:selImgView];
    [selImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-Zoom(12));
        make.width.height.mas_equalTo(Zoom(15));
    }];
    
    return view;
}

- (NSMutableArray *)payWayViewsArray
{
    if (!_payWayViewsArray)
    {
        self.payWayViewsArray = NSMutableArray.array;
    }
    return _payWayViewsArray;
}

@end


